package com.atguigu.controller;

import com.atguigu.common.result.R;
import com.atguigu.service.DictService;
import com.atguigu.yygh.model.cmn.Dict;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
//@CrossOrigin
@RequestMapping("/admin/cmn/dict")
public class DictController {
    @Autowired
    private DictService dictService;


    @GetMapping("findChildData/{id}")
    public R findChildData(@PathVariable Long id) {
        List<Dict> list = dictService.findChildData(id);
        return R.ok().data("list", list);
    }

    //文件导出
    @GetMapping(value = "/exportData")
    public void exportData(HttpServletResponse response) {

        dictService.exportData(response);
    }

    //文件导入
    @PostMapping("importData")
    public R importData(MultipartFile file) {
        dictService.importDictData(file);
        return R.ok();
    }

    // http://localhost:8202/admin/cmn/dict/getName/Hostype/1    三级甲等
    @GetMapping(value = "/getName/{parentDictCode}/{value}")
    public String getName(@PathVariable("parentDictCode") String parentDictCode,@PathVariable("value") String value){
        return dictService.getNameByParentDictCodeAndValue(parentDictCode, value);
    }
    // http://localhost:8202/admin/cmn/dict/getName/130100		石家庄市
    @GetMapping(value = "/getName/{value}")
    public String getName(@PathVariable("value") String value) {
        return dictService.getNameByParentDictCodeAndValue("", value);
    }

    @GetMapping("/findByDictCode/{dictCode}")
    public R  findByDictCode (@PathVariable String dictCode){
        List<Dict> list=dictService.findByDictCode(dictCode);
        return  R.ok().data("list",list);

    }


}
